package com.xiaochuan.utils;


import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;

public class MybatisPlusGreen {

    public static void main(String[] args) {

        AutoGenerator mpg = new AutoGenerator();//生成一个代码生成器

        //全局配置
        GlobalConfig gc = new GlobalConfig();

        //获取系统目录
        gc.setOutputDir(System.getProperty("user.dir")+"/src/main/java");
        //作者
        gc.setAuthor("经典");
        //打开资源管理器
        gc.setOpen(false);
        //是否覆盖原本生成的
        gc.setFileOverride(false);
        //去serviceI前缀
        gc.setServiceName("%sService");
        gc.setIdType(IdType.AUTO);
        //开启接口测试
        gc.setSwagger2(true);
        mpg.setGlobalConfig(gc);
        //设置数据源
        DataSourceConfig sourceConfig = new DataSourceConfig();
        sourceConfig.setUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false&autoReconnect=true&serverTimezone=UTC&zeroDateTimeBehavior=CONVERT_TO_NULL");
        sourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
        sourceConfig.setUsername("root");
        sourceConfig.setPassword("123456");
        sourceConfig.setDbType(DbType.MYSQL);
        mpg.setDataSource(sourceConfig);

        //配置包名
        PackageConfig config = new PackageConfig();
        //模块
//        config.setModuleName("model");

        config.setParent("com.xiaochuan");
        config.setMapper("mapper");
        config.setEntity("pojo");
        config.setController("controller");
        config.setService("service");

        mpg.setPackageInfo(config);
        //策略配置
        StrategyConfig strategy = new StrategyConfig();
//        strategy.setInclude("apk","app_ota","chanels_info","cmd_parameter","cmdgroup","cmdgroup_cmd","command","desktop_dmodule","icon","icon_group","iknow_user","image","image_group"
//                ,"maintaince_record","menu","menu_group","menu_res","music","music_group","ota_info","purview","qa","qa_group","robot_bom","robot_charge_config","robot_config"
//                ,"robot_desktop_config","robot_dmodule_config","robot_explain_config","robot_explain_status","robot_explainpoint_config","robot_face_config","robot_greeting_config"
//                ,"robot_greeting_status","robot_img","robot_lowpowercharge_config","robot_patrol_status","robot_patrolpoint_config","robot_point_config","robot_product","robot_purchase"
//                ,"robot_purchase_category","robot_qa","robot_status","robot_vip_config","robot_map","robot_navi_status","robot_ordernew","robot_ota","robot_patrol_config","role_purview"
//                ,"sequence","storelocation","tbl_attr_key","tbl_attr_value","tbl_category","tbl_category_attr","tbl_goods","tbl_goods_attr_value","tbl_goods_group","tbl_goods_photo"
//                ,"robot_voice_info","robot_work_config","robot_work_status","role","tbl_groups","tbl_robot_goods","user","user_reply","user_robot","video","video_group"
//        );//设置表名
        strategy.setInclude("user","user_role","user_jurisdiction","user_role_jurisdiction","user_sys_privilege");
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        //逻辑删除
        strategy.setLogicDeleteFieldName("deleted");
        //自动填充配置
        TableFill createTime = new TableFill("create_time", FieldFill.INSERT);
        TableFill updateTime = new TableFill("update_time", FieldFill.INSERT_UPDATE);
        ArrayList<TableFill> list = new ArrayList<>();
        list.add(createTime);
        list.add(updateTime);
        strategy.setTableFillList(list);

        //乐观锁
        strategy.setVersionFieldName("version");
        //驼峰命名
        strategy.setRestControllerStyle(true);
        strategy.setControllerMappingHyphenStyle(true);
        mpg.setStrategy(strategy);
        mpg.execute();


    }



}
